package com.yang.action;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringBufferInputStream;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import com.yang.LoginService;
import com.yang.User;
import com.yang.service.ServiceManager;
import com.yang.util.CreateGra;

@SuppressWarnings("deprecation")
public class LoginAction extends ActionSupport implements SessionAware,
		ServletResponseAware {

	private static final long serialVersionUID = -1669542551147918284L;
	private String name;
	private String password;
	private String id;
	private LoginService ls = new LoginService();
	private ServiceManager service = ServiceManager.newInstance();
	User u = null;
	private Map<String, Object> session;

	private HttpServletResponse resp;
	private InputStream inputStream;

	@Override
	public String execute() throws Exception {
		password=DigestUtils.md5Hex(password);
		u = ls.login(name, password);
		setSession(session);
		if (u != null&&u.getPower()!=2) {
			session.put("user", u);
			inputStream = new StringBufferInputStream("ok");
			return SUCCESS;
		} else {
			inputStream = new StringBufferInputStream("error");
			return "index";
		}
	}
	public String checkName() throws Exception{
		u=ls.findByName(name);
		if (u != null) {
			inputStream = new StringBufferInputStream("error");
			return "index";
		} else {
			inputStream = new StringBufferInputStream("ok");
			return SUCCESS;
		}
	}
	public String reg() throws Exception{
		password=DigestUtils.md5Hex(password);
		boolean b=ls.addUser(name,password);
		u=ls.login(name, password);
		session.put("user", u);
		if (!(b&&u != null)) {
			inputStream = new StringBufferInputStream("error");
			return "index";
		} else {
			session.put("user", u);
			inputStream = new StringBufferInputStream("ok");
			return SUCCESS;
		}
	}
	public String addmin() throws Exception{
		boolean b=ls.addmin(Integer.parseInt(id));
		if (b) {
			inputStream = new StringBufferInputStream("ok");
			return SUCCESS;
		} else {
			inputStream = new StringBufferInputStream("error");
			return "index";
		}
	}
	public String delUser()throws Exception{
		boolean b=ls.delUser(Integer.parseInt(id));
		if(b){
			inputStream = new StringBufferInputStream("ok");
			return SUCCESS;
		}else{
			inputStream = new StringBufferInputStream("error");
			return ERROR;
		}
	}
    public InputStream getInputStream() {
        return inputStream;
    }

	public String exit() throws Exception {
		session.remove("user");
		return SUCCESS;
	}

	public String getName() {
		return name;
	}

	public String getPassword() {
		return password;
	}

	public String img() throws Exception {
		OutputStream os = resp.getOutputStream();
		CreateGra.createImg(120, 120, os, "hello");
		return null;
	}

	public String restart() throws Exception {
		boolean flag = service.reStart();
		if (flag) {
			return SUCCESS;
		} else {
			return "index";
		}
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public void setServletResponse(HttpServletResponse resp) {
		this.resp = resp;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String start() throws Exception {
		boolean flag = service.start();
		if (flag) {
			return SUCCESS;
		} else {
			return "index";
		}
	}

	public String stop() throws Exception {
		boolean flag = service.stop();
		if (flag) {
			return SUCCESS;
		} else {
			return "index";
		}
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
}
